/*
 * @Author: sympsel sympsel@outlook.com
 * @Date: 2025-07-16 20:31:16
 * @LastEditors: sympsel sympsel@outlook.com
 * @LastEditTime: 2025-07-17 15:46:42
 * @FilePath: \work25_7_16_2\src\Stack.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Stack.h"

// 初始化栈 
Stack* StackInit() {
    Stack* stack = MALLOC(Stack, 1);
    stack->_capacity = DEFAULT_CAPACITY;
    stack->_top = -1;
    stack->_data = MALLOC(STDataType, DEFAULT_CAPACITY);
    return stack;
}
// 入栈 
void StackPush(Stack* ps, STDataType data) {
    if (ps->_top + 1 >= ps->_capacity) {
        ps->_capacity += ps->_capacity / 2;
        ps->_data = REALLOC(ps->_data, int, ps->_capacity);
    }

    ps->_data[++ps->_top] = data;
    
}
// 出栈 
void StackPop(Stack* ps) {
    if (!StackEmpty(ps)) {
        ps->_top--;
    }
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps) {
    return ps->_data[ps->_top];
} 
// 获取栈中有效元素个数 
int StackSize(Stack* ps) {
    return ps->_top + 1;
}
// 检测栈是否为空，如果为空返回非零结果，如果不为空返回0 
bool StackEmpty(Stack* ps) {
    return ps->_top == -1;
}
// 销毁栈 
void StackDestroy(Stack* ps) {
    free(ps->_data);
    free(ps);
}